Page({
  data: {
    calculationType: 'direct',
    pricePerUnit: '',
    calorificValue: '',
    coalPrice: '',
    transportFee: '',
    taxRate: 0.09,
    result: null
  },

  onLoad(options) {
    // 监听来自历史记录页面的数据
    const eventChannel = this.getOpenerEventChannel()
    if (eventChannel) {
      eventChannel.on('useHistoryRecord', (data) => {
        this.setData({
          calculationType: data.calculationType,
          pricePerUnit: data.pricePerUnit || '',
          calorificValue: data.calorificValue || '',
          coalPrice: data.coalPrice || '',
          transportFee: data.transportFee || '',
          taxRate: data.taxRate || 0.09,
          result: data.result || null
        })
      })
    }
  },

  // 切换计算方式
  switchCalculationType(e) {
    this.setData({
      calculationType: e.detail.value,
      coalPrice: '',
      pricePerUnit: '',
      result: null
    })
  },

  // 输入处理函数
  onPricePerUnitInput(e) {
    this.setData({ 
      pricePerUnit: e.detail.value 
    }, () => {
      // 自动计算入厂煤价
      if (this.data.calculationType === 'perUnit' && this.data.pricePerUnit && this.data.calorificValue) {
        this.calculateCoalPrice()
      }
    })
  },

  onCalorificValueInput(e) {
    this.setData({ 
      calorificValue: e.detail.value 
    }, () => {
      // 自动计算入厂煤价
      if (this.data.calculationType === 'perUnit' && this.data.pricePerUnit && this.data.calorificValue) {
        this.calculateCoalPrice()
      }
    })
  },

  onCoalPriceInput(e) {
    this.setData({ coalPrice: e.detail.value })
  },

  onTransportFeeInput(e) {
    this.setData({ transportFee: e.detail.value })
  },

  onTaxRateChange(e) {
    this.setData({ taxRate: parseFloat(e.detail.value) })
  },

  // 计算入厂煤价
  calculateCoalPrice() {
    const { pricePerUnit, calorificValue } = this.data
    if (!pricePerUnit || !calorificValue) {
      return
    }

    const coalPrice = (parseFloat(pricePerUnit) * parseFloat(calorificValue)).toFixed(2)
    this.setData({ coalPrice })
  },

  // 计算标煤单价
  calculateStandardCoal() {
    const { coalPrice, transportFee, taxRate, calorificValue, calculationType } = this.data
    
    // 如果是每卡单价计算模式，先计算入厂煤价
    if (calculationType === 'perUnit') {
      this.calculateCoalPrice()
    }
    
    if (!coalPrice || !transportFee || !calorificValue) {
      wx.showToast({
        title: '请输入完整数据',
        icon: 'none'
      })
      return
    }

    // 计算不含税煤价
    const coalPriceWithoutTax = parseFloat(coalPrice) / 1.13
    // 计算不含税运费
    const transportFeeWithoutTax = parseFloat(transportFee) / (1 + parseFloat(taxRate))
    
    // 计算标煤单价
    const standardCoalPrice = (coalPriceWithoutTax + transportFeeWithoutTax) * (7000 / parseFloat(calorificValue))
    
    this.setData({
      result: {
        standardCoalPrice: standardCoalPrice.toFixed(2),
        coalPriceWithoutTax: coalPriceWithoutTax.toFixed(2),
        transportFeeWithoutTax: transportFeeWithoutTax.toFixed(2)
      }
    })

    // 保存到历史记录
    this.saveToHistory()
  },

  // 保存计算记录
  saveToHistory() {
    const history = wx.getStorageSync('calculation_history') || []
    const record = {
      ...this.data,
      timestamp: new Date().getTime()
    }
    history.unshift(record)
    wx.setStorageSync('calculation_history', history.slice(0, 50))
  }
})
